package com.gengyun.gengyunacquistion.controller;

import com.gengyun.gengyunacquistion.common.Result;
import com.gengyun.gengyunacquistion.scheduled.DataAcquisition;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/acquisition")
public class ScheduledController {

    @Qualifier("schedulerFactoryBean")
    @Autowired
    private Scheduler scheduler;

    @RequestMapping("/start")
    public Result startSchedule() {
        try {
            scheduler.resumeJob(JobKey.jobKey("job1"));
            return new Result("开始任务成功",0,"开始任务成功");
        } catch (SchedulerException e) {
            e.printStackTrace();
            return new Result("开始任务失败",1,"开始任务失败");
        }
//        return new Result("测试",0,"测试");
    }

    @RequestMapping("/pause")
    public Result pauseSchedule() {
        try {
            JobKey jobKey = JobKey.jobKey("job1");
            scheduler.pauseJob(jobKey);
            return new Result("暂停任务成功",0,"暂停任务成功");
        } catch (SchedulerException e) {
            e.printStackTrace();
            return new Result("暂停任务失败",1,"暂停任务失败");
        }
    }

    @RequestMapping("/resume")
    public Result resumeSchedule() {
        try {
            scheduler.resumeJob(JobKey.jobKey("job1"));
            return new Result("恢复任务成功",0,"恢复任务成功");
        } catch (SchedulerException e) {
            e.printStackTrace();
            return new Result("恢复任务失败",1,"恢复任务失败");
        }
    }

    @RequestMapping("/stop")
    public Result stopSchedule() {
//        try {
//            scheduler.pauseTrigger(new TriggerKey("trigger1"));
//            scheduler.unscheduleJob(new TriggerKey("trigger1"));
//            scheduler.deleteJob(JobKey.jobKey("job1"));
//            return new Result("停止任务成功",0,"停止任务成功");
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//            return new Result("停止任务失败",1,"停止任务失败");
//        }
        try {
            scheduler.resumeJob(JobKey.jobKey("job1"));
            return new Result("停止任务成功",0,"停止任务成功");
        } catch (SchedulerException e) {
            e.printStackTrace();
            return new Result("停止任务失败",1,"停止任务失败");
        }
    }
}
